/*
 * Copyright 2006-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.czeczek.bossa;

import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;

import com.czeczek.domain.Quote;



public class TradeFieldSetMapper implements FieldSetMapper<Quote> {
	
	private boolean isIntraData;
	
	private DateTimeFormatter dateFormat;
	private DateTimeFormatter timeFormat;
	
	public static final String SYMBOL_COLUMN = "Symbol";
	public static final String ZERO_COLUMN = "Zero";
	public static final String DATE_COLUMN = "Date";
	public static final String TIME_COLUMN = "Time";
	public static final String OPEN_PRICE_COLUMN = "OpenPrice";
	public static final String HIGH_PRICE_COLUMN = "HighPrice";
	public static final String LOW_PRICE_COLUMN = "LowPrice";
	public static final String CLOSE_PRICE_COLUMN = "ClosePrice";
	public static final String VOLUME_PRICE_COLUMN = "Volume";
	public static final String OPEN_INT_COLUMN = "OpenInt";	



	public String getDateFormat() {
		return dateFormat.toString();
	}



	public void setDateFormat(String dateFormat) {
		this.dateFormat = DateTimeFormat.forPattern(dateFormat);
	}



	public String getTimeFormat() {
		return timeFormat.toString();
	}



	public void setTimeFormat(String timeFormat) {
		this.timeFormat = DateTimeFormat.forPattern(timeFormat);
	}



	public boolean isIntraData() {
		return isIntraData;
	}



	public void setIntraData(boolean isIntraData) {
		this.isIntraData = isIntraData;
	}



	public Quote mapFieldSet(FieldSet fieldSet) {
    	Quote trade = new Quote();
    	trade.setSymbol(fieldSet.readString(SYMBOL_COLUMN));
    	trade.setDate(dateFormat.parseDateTime(fieldSet.readString(DATE_COLUMN)).toLocalDate());
    	if (isIntraData) {
        	trade.setTime(timeFormat.parseDateTime(fieldSet.readString(TIME_COLUMN)).toLocalTime());    		
    	}
        trade.setOpenPrice(fieldSet.readBigDecimal(OPEN_PRICE_COLUMN));
        trade.setHighPrice(fieldSet.readBigDecimal(HIGH_PRICE_COLUMN));
        trade.setLowPrice(fieldSet.readBigDecimal(LOW_PRICE_COLUMN));
        trade.setClosePrice(fieldSet.readBigDecimal(CLOSE_PRICE_COLUMN));
        trade.setVolume(fieldSet.readBigDecimal(VOLUME_PRICE_COLUMN));
        trade.setOpenInterest(fieldSet.readBigDecimal(OPEN_INT_COLUMN).toBigInteger());        
        
        return trade;
    }
}
